﻿//using System;
//using System.Collections.Generic;
//using System.Data.Entity;
//using System.Linq;
//using System.Reflection;
//using System.Text;
//using System.Threading.Tasks;
//using System.Web.Mvc;
//using Developer.Entity;
//using Super.Framework.Common;
//using Super.Framework.Common.Web;
//using Super.Framework.EF;
//using Super.Framework.MVC;

//namespace Super.Service
//{
//    public class AuthService
//    {
//        #region 权限基础方法

//        /// <summary>
//        /// 获取权限项列表
//        /// </summary>
//        /// <param name="pageCount"></param>
//        /// <param name="pageIndex"></param>
//        /// <param name="pageSize"></param>
//        /// <param name="itemCount"></param>
//        /// <param name="keyword"></param>
//        /// <returns></returns>
//        public static List<ActionPermission> GetActionPermissionList(out int itemCount, out int pageCount, int pageIndex, int pageSize, string keyword)
//        {

//            var items = BaseService<ActionPermission>.GetAll().Where(a => a.IsAbort == false);
//            if (keyword.IsNotNullOrEmpty())
//            {
//                items = items.Where(a => a.Description.Contains(keyword) || a.ControllerName.Contains(keyword) ||
//                                                 a.ActionName.Contains(keyword));
//            }
//            var list = items.Page(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: pageSize);

//            return list;
//        }

//        /// <summary>
//        /// 获取角色
//        /// </summary>
//        /// <param name="itemCount"></param>
//        /// <param name="pageCount"></param>
//        /// <param name="pageIndex"></param>
//        /// <param name="pageSize"></param>
//        /// <param name="keyword"></param>
//        /// <returns></returns>
//        public static List<Role> GetRoleList(out int itemCount, out int pageCount, int pageIndex, int pageSize, string keyword)
//        {
//            var items = BaseService<Role>.GetAll().Where(a => a.IsAbort == false);
//            if (keyword.IsNotNullOrEmpty())
//            {
//                items = items.Where(a => a.Name.Contains(keyword) || a.Description.Contains(keyword));
//            }
//            var list = items.OrderByDescending(a => a.CreateTime).Page(out itemCount, out pageCount, pageIndex: pageIndex, pageSize: pageSize);

//            return list;
//        }
//        /// <summary>
//        /// 更新权限项
//        /// </summary>
//        /// <param name="controllerType"></param>
//        public static void UpdateActionPermission(Type controllerType)
//        {
//            var result = new List<ActionPermission>();

//            var types = Assembly.GetAssembly(controllerType).GetTypes();

//            var actionPermissionList = SysInfo.DB.ActionPermission.Where(a=>a.IsAbort==false).ToList();

//            foreach (var type in types)
//            {
//                if (type?.BaseType == null)
//                {
//                    continue;
//                }

//                if (type.BaseType.Name == "BaseController" && type.Name != "BaseController")//如果是Controller
//                {
//                    var controllerAttribute = (AuthDescriptionAttribute)(type.GetCustomAttributes(false).FirstOrDefault(x => x is AuthDescriptionAttribute));
//                    if (controllerAttribute == null)
//                    {
//                        continue;
//                    }

//                    var controllerName = type.Name.Substring(0, type.Name.Length - 10); // 去掉“Controller”后缀

//                    var parentAp =
//                        actionPermissionList.FirstOrDefault(
//                            a => a.ControllerName == controllerName && a.ParentId == 0 && a.Type == controllerAttribute.Type && a.IsAbort == false);
//                    if (parentAp == null)
//                    {
//                        parentAp = new ActionPermission();
//                        parentAp.Description = controllerAttribute.Name;
//                        parentAp.ParentId = 0;
//                        parentAp.Type = controllerAttribute.Type;
//                        parentAp.CreateTime = DateTime.Now;
//                        parentAp.ControllerName = controllerName; // 去掉“Controller”后缀
//                        SysInfo.DB.ActionPermission.Add(parentAp);
//                        SysInfo.DB.SaveChanges();
//                    }
//                    result.Add(parentAp);
//                    var members = type.GetMethods();
//                    foreach (var member in members)
//                    {
//                        if (member.DeclaringType.Name != "BaseController" && member.DeclaringType.Name != "CommonController" && (member.ReturnType.Name == "ActionResult" || member.ReturnType.Name == "JsonResult"))//如果是Action
//                        {
//                            var actionAttribute = (AuthDescriptionAttribute)(member.GetCustomAttributes(false).FirstOrDefault(x => x is AuthDescriptionAttribute));
//                            var postAttribute = (HttpPostAttribute)(member.GetCustomAttributes(false).FirstOrDefault(x => x is HttpPostAttribute));

//                            if (actionAttribute == null)
//                            {
//                                continue;
//                            }

//                            var isPost = postAttribute != null;

//                            var ap =
//                                actionPermissionList.FirstOrDefault(
//                                    a =>
//                                        a.ControllerName == controllerName && a.ActionName == member.Name &&
//                                        a.Description == actionAttribute.Name && a.Type == actionAttribute.Type && a.IsPost == isPost && a.IsAbort == false);
//                            if (ap == null)
//                            {
//                                ap = new ActionPermission();
//                                ap.ActionName = member.Name;
//                                ap.ControllerName = controllerName; //member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10); // 去掉“Controller”后缀
//                                ap.Description = actionAttribute.Name;
//                                ap.ParentId = parentAp.Id;
//                                ap.Type = actionAttribute.Type;
//                                ap.CreateTime = DateTime.Now;
//                                ap.IsPost = isPost;
//                                SysInfo.DB.ActionPermission.Add(ap);
//                            }

//                            result.Add(ap);
//                        }

//                    }

//                    SysInfo.DB.SaveChanges();

//                    //actionPermissionList = SysInfo.DB.ActionPermission.ToList();

//                    //foreach (var item in actionPermissionList)
//                    //{
//                    //    if (result.Any(a =>
//                    //        a.ControllerName == controllerName && a.ActionName == item.ActionName &&
//                    //        a.Description == item.Description && a.Type == item.Type &&
//                    //        a.IsPost == item.IsPost))
//                    //    {
//                    //        SysInfo.DB.ActionPermission.Remove(item);
//                    //    }
//                    //}

//                    //SysInfo.DB.SaveChanges();


//                }
//            }
//        }

//        /// <summary>
//        /// 删除权限项
//        /// </summary>
//        /// <param name="entities"></param>
//        public static void RemoveActionPermission(IEnumerable<ActionPermissionRole> entities)
//        {
//            SysInfo.DB.ActionPermissionRole.RemoveRange(entities);
//            SysInfo.DB.SaveChanges();
//        }
//        #endregion
//    }
//}
